<?php
namespace api\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\web\IdentityInterface;

class User extends ActiveRecord implements IdentityInterface {
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    public function fields()
    {
        return [
            'id',
            'access_token',
            'nickname',
            'avatar',
            'gender',
            'mobile',
            'role',
            'status'
        ];
    }

    /*
     * 以下几个方法是验证用户是否登录
     */
    public static function findIdentity($id)
    {
        return static::findOne(['user_id' => $id]);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['access_token' => $token]);
    }

    public function getAuthKey()
    {
        return $this->auth_key;
    }

    public function loginByAccessToken($token, $type) {
        return static::findIdentityByAccessToken($token, $type);
    }

    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    public function getId()
    {
        return $this->getPrimaryKey();
    }

}
